﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LIMS.Entities;
using LIMS.DataAccess;
using System.Collections;
using System.Reflection;
using Castle.Core.Logging;
using NHibernate.Criterion;

namespace LIMS.Service
{
    public class DepartmentManagementService : IDepartmentManagementService
    {
        //Private DBA interfaces
        private IDaoFactory oDBFactory;
        private IDepartmentDAO oDepartmentDBA;
        private ILogger logger;
        private IPlantManagementService plantservice;
        
        //Constructor
        public DepartmentManagementService(IDaoFactory oDBOp, ILogger oLogger, IPlantManagementService plantservice)
        {
            this.oDBFactory = oDBOp;
            this.oDepartmentDBA = oDBFactory.GetDepartmentDAO();
            this.logger = oLogger;
            this.plantservice = plantservice;
        }
        #region IDepartmentManagementService Members

        public List<Department> GetAllDepartments()
        {
            List<Department> myDepartments = new List<Department>();// oDepartmentDBA.GetAll();


            try
            {
                ICriterion oCriteria = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                myDepartments = oDepartmentDBA.GetByCriteria(oCriteria);
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting all user Department.", ex);
                //    return null;
            }
            return myDepartments;
        }

        public bool Save(Department newDepartment)
        {
            return oDepartmentDBA.QuickSaveOrUpdate(ref newDepartment);
        }

        public bool Delete(Department dDepartment)
        {
            return oDepartmentDBA.QuickDelete(dDepartment);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="oUser"></param>
        /// <returns></returns>
        public List<Department> GetHODDepartment(User oUser)
        {
            List<Department> myDepartments = new List<Department>();// oDepartmentDBA.GetAll();

            List<string> oValue = new List<string>();

            string[] odepartment = oUser.DepartmentId.Split(',');
            foreach (string sValue in odepartment)
            {
                oValue.Add(sValue);
            }
            try
            {
                ICriterion oCriteria = Expression.In("Id", oValue);
                myDepartments = oDepartmentDBA.GetByCriteria(oCriteria);
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting all user Department.", ex);
                //    return null;
            }
            return myDepartments;
        }

        #endregion

        #region IDepartmentManagementService Members


        public List<Department> GetAllDepartmentsByPlantId(string sPlantId)
        {
            List<Department> myDepartments = new List<Department>();

            try
            {
                string[] aPlantId = new string[0];
                aPlantId = sPlantId.Split(Convert.ToChar(","));
                ICriterion oCriteriaPlantId = Expression.In("PlantId", aPlantId);
                ICriterion oCriteriaStatus = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                ICriterion oCriteria = Expression.And(oCriteriaPlantId, oCriteriaStatus);
                myDepartments = oDepartmentDBA.GetByCriteria(oCriteria);
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting Department by plant.", ex);
                //    return null;
            }
            return myDepartments;
        }

        public List<Department> GetAllDepartmentsByPlantId(int iPlantId)
        {
            List<Department> myDepartments = new List<Department>();

            try
            {
                ICriterion oCriteriaPlantId = Expression.Eq("PlantId", iPlantId);
                ICriterion oCriteriaStatus = Expression.Eq("Status", Resource.ACTIVE_STATUS);
                ICriterion oCriteria = Expression.And(oCriteriaPlantId, oCriteriaStatus);
                myDepartments = oDepartmentDBA.GetByCriteria(oCriteria);
            }
            catch (Exception ex)
            {
                logger.Error("Error while getting Department by plant.", ex);
                //    return null;
            }
            return myDepartments;
        }

        /// <summary>
        /// GetAllSitesForGrid
        /// </summary>
        /// <param name="oGrdData"></param>
        /// <returns></returns>
        public List<Department> GetAllDepartmentForGrid(ref GridViewData oGrdData)
        {

            List<Department> departmentList = new List<Department>();
            departmentList = oDepartmentDBA.GetAllForGridView(ref oGrdData);

            foreach (Department oDepartment in departmentList)
            {
                oDepartment.PlantName = plantservice.GetPlantNameByID(oDepartment.PlantId);

            }
            return oDepartmentDBA.GetAllForGridView(ref oGrdData);
        }



        #endregion
    }
}
